package org.guajava.controller.system;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.guajava.common.annotation.Log;
import org.guajava.common.base.AjaxResult;
import org.guajava.common.enums.BusinessType;
import org.guajava.domain.DictData;
import org.guajava.domain.DictType;
import org.guajava.framework.web.base.BaseController;
import org.guajava.framework.web.page.TableDataInfo;
import org.guajava.service.IDictTypeService;

/**
 * 字典类型表 前端控制器
 *
 * @author xieke
 * @email 949118693@qq.com
 * @date 2019-01-11 12:25:42
 */
@Controller
@RequestMapping("/system/dict")
public class SysDictTypeController extends BaseController{

    private String prefix = "/system/dict";

    @Autowired
    private IDictTypeService dictTypeService;

    @GetMapping()
    public String post() {
        return prefix + "/list";
    }

    @RequestMapping("/*")
    public void toHtml(){
    }

    @PostMapping("/listData")
    @ResponseBody
    public TableDataInfo listData(DictType dictType){
        startPage();
        List<DictType> list = dictTypeService.selectDictTypeList(dictType);
        return getDataTable(list);
    }

    @PostMapping("/datalist")
    @ResponseBody
    public TableDataInfo dictDataList(@RequestParam Map<String,String> map){
        startPage();
        List<DictData> list = dictTypeService.selectDictDataList(map);
        return getDataTable(list);
    }

    @Log(title = "字典类型表添加", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult addSave(DictType dictType){
        return toAjax(dictTypeService.insertDictType(dictType));
    }


    @Log(title = "字典类型表修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(DictType dictType) {
        return toAjax(dictTypeService.updateDictType(dictType));
    }

    @Log(title = "字典类型表删除", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String[] ids) {
        try {
            return toAjax(dictTypeService.deleteDictTypeByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @Log(title = "字典数据表添加", businessType = BusinessType.INSERT)
    @PostMapping("/addDictData")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult addSave(DictData dictData){
        return toAjax(dictTypeService.insertDictData(dictData));
    }


    @Log(title = "字典数据表修改", businessType = BusinessType.UPDATE)
    @PostMapping("/editDictData")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(DictData dictData) {
        return toAjax(dictTypeService.updateDictData(dictData));
    }

    @Log(title = "字典数据表删除", businessType = BusinessType.DELETE)
    @PostMapping("/removeDictData")
    @ResponseBody
    public AjaxResult removeDictData(String[] ids) {
        try {
            return toAjax(dictTypeService.deleteDictDataByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}
